package itheima.ui;

import itheima.Constant;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class LoginFrame extends JFrame {
    private JTextField usernameField;
    private JButton loginButton;
    private JButton cancelButton;

    private Socket socket;

    public LoginFrame() {
        setTitle("登录界面");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        // 不允许修改窗口大小
        setResizable(false);

        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);

        JLabel titleLabel = new JLabel("欢迎登录");
        titleLabel.setFont(new Font("楷体", Font.BOLD, 24));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        panel.add(titleLabel, gbc);

        JLabel usernameLabel = new JLabel("用户名:");
        usernameLabel.setFont(new Font("楷体", Font.PLAIN, 16));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(usernameLabel, gbc);

        usernameField = new JTextField(20);
        usernameField.setFont(new Font("楷体", Font.PLAIN, 16));
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0;
        panel.add(usernameField, gbc);

        loginButton = new JButton("登录");
        loginButton.setFont(new Font("楷体", Font.BOLD, 16));
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weightx = 0.5;
        panel.add(loginButton, gbc);

        cancelButton = new JButton("取消");
        cancelButton.setFont(new Font("楷体", Font.BOLD, 16));
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weightx = 0.5;
        panel.add(cancelButton, gbc);

        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                if (username.isEmpty()) {
                    JOptionPane.showMessageDialog(LoginFrame.this, "用户名不能为空", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                else if (username.length() > 20) {
                    JOptionPane.showMessageDialog(LoginFrame.this, "用户名不能超过20个字符", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                // 确认名称无误后登录
                login(username);

                new ClientChatFrame(username, socket);
                dispose(); // 关闭当前窗口
            }
        });

        cancelButton.addActionListener(e -> dispose());

        add(panel);
        setVisible(true);
    }

    private void login(String username) {
        try {
            // 1. 创建Socket对象，连接服务器
            socket = new Socket(Constant.IP, Constant.PORT);
            // 2. 获取Socket输出流，发送数据给服务器
            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
            dos.writeInt(1);
            dos.writeUTF(username);
            dos.flush();


        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }
}
